﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once

#include <thread>
#include "GmUiInplaceEditCommon.h"
#include "OwnerPtr.h"
#include "GbmpGuid.h"
#include "GmCommunicationEventTypes.h"

#define PORT_ASSIGNED_BY_SYSTEM 0

namespace gcmp
{
    class ProcessProtocolEvent;
    class GmProcessServerEvents;
    class IProcessServerEventHandle;
    class SocketServer;
    //进程服务器，负责监听族编辑发来的信息，并将信息post到主线程，这里需要注册handle进来
    class GM_UI_INPLACE_EDIT_COMMON_EXPORT ProcessServer
    {
    private:
        ProcessServer();
    public:
        static ProcessServer* GetInstance();
        ~ProcessServer();

        //开始监听了，服务器开始运行
        bool Listen(uint32_t port, uint32_t clientSIze);

        uint16_t GetPort() const;
        GUID GetServerGuid() const;

        //
        void Stop();
        bool IsStop()const;

        bool RegisterProcessServerEventHandle(FunctionType functionType, OwnerPtr<IProcessServerEventHandle> opHandler);
    protected:
        void Run(OwnerPtr<SocketServer> opSocketServer);
        bool PostMessages(const std::vector<char>& buffer);

        bool m_isStoped;
        uint16_t m_port;
        std::thread m_serverThread;
        OwnerPtr<GmProcessServerEvents> m_opServerEvents;
        GUID m_serverGuid;
        SocketServer*volatile  m_pSocketServerRef;
    };
}
